package demo2;

public class MySingleList implements IList {

    static class ListNode {//外面是链表,里面要求是整体,即节点,则使用内部类
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    //链表的属性,头节点
    public ListNode head;
    public void createNode() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;

        this.head = node1;
    }

    /**
     * 头插
     * @param data
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = this.head;
        this.head = node;
    }

    /**
     * 尾插
     * @param data
     */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    /**
     * 在index下标位置插入节点(第一个节点为0)
     * @param index
     * @param data
     */
    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            throw new IndexException("下标不合法!");
        }

        if(index == 0) {
            addFirst(data);
            return;
        }

        if(index == size()) {
            addLast(data);
            return;
        }
        //中间位置插入
        ListNode cur = searchPrevNode(index);
        ListNode node = new ListNode(data);

        node.next = cur.next;
        cur.next = node;
    }
    private ListNode searchPrevNode(int index) {
        ListNode cur = head;
        int count = 0;
        //找到下标前一个节点
        while(count != index) {
            cur = cur.next;
            count++;
        }

        return cur;
    }

    /**
     * 判断链表中是否存在val = key的节点
     * @param key
     * @return
     */
    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }

        ListNode cur = head;

        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    /**
     * 删除节点key
     * @param key
     */
    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }

        if(head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findPrevKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }

    /**
     * 删除所有key节点
     *      1.删除除头节点以外节点
     *              使用两个节点进行处理(前驱 待删)
     *      2.处理头节点
     * @param key
     */
    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        //如果先处理head需要使用循环,因为可能头后面还是需要删的节点
//        while(head.val == key) {
//            head = head.next;
//        }

        ListNode cur = head;
        ListNode prev  = head;
        //处理后面节点
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {//cur.val != key
                prev = cur;
                cur = cur.next;
            }
        }
        //后面处理,使用if
        if(head.val == key) {
            head = head.next;
        }
    }

    /**
     * 求链表节点个数
     * @return
     */
    @Override
    public int size() {
        if(head == null) {
            return 0;
        }

        int count = 0;
        ListNode cur = head;

        while(cur != null) {
            count++;
            cur = cur.next;
        }

        return count;
    }

    @Override
    public void clear() {
        this.head = null;
    }

    /**
     * 打印节点
     */
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
